// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package androidx.gdata.blogger.xml;

import org.w3c.dom.Node;

import java.util.List;

import androidx.gdata.xml.ThingWithLinksDeserializer;
import androidx.gdata.xml.CategoryDeserializer;
import androidx.gdata.xml.AuthorDeserializer;
import androidx.gdata.LinkUrl;
import androidx.gdata.Category;
import androidx.gdata.Author;
import androidx.gdata.blogger.BlogEntry;
import androidx.gdata.blogger.BlogContent;
import androidx.gdata.blogger.AppEdited;
import androidx.gdata.blogger.ThreadTotal;
import androidx.gdata.blogger.AppControl;

public class BlogEntryDeserializer extends ThingWithLinksDeserializer<BlogEntry> {
  private static CategoryDeserializer categoryBuilder = new CategoryDeserializer();
  private static AuthorDeserializer authorBuilder = new AuthorDeserializer();
  private static BlogContentDeserializer contentBuilder = new BlogContentDeserializer();
  private static AppEditedDeserializer appEditedBuilder = new AppEditedDeserializer();
  private static AppControlDeserializer appControlBuilder = new AppControlDeserializer();
  private static ThreadTotalDeserializer ttBuilder = new ThreadTotalDeserializer();

  public BlogEntry build(Node entryNode) {
    node = entryNode;
    String id = getText("id");
    String published = getText("published");
    String updated = getText("updated");
    AppEdited appEdited = null;
    Node appEditedNode = getChildNode("app:edited");
    appEdited = appEditedBuilder.build(appEditedNode);
    AppControl appControl = null;
    Node appControlNode = getChildNode("app:control");
    if (appControlNode != null) {
      appControl = appControlBuilder.build(appControlNode);
    }
    ThreadTotal tt = null;
    Node ttNode = getChildNode("thr:total");
    tt = ttBuilder.build(ttNode);
    Category[] categories = null;
    List<Node> categoryNodes = getChildNodes("category");
    if (categoryNodes != null) {
      categories = new Category[categoryNodes.size()];
      int i = 0;
      for (Node categoryNode : categoryNodes) {
        categories[i++] = categoryBuilder.build(categoryNode);
      }
    }
    Node authorNode = getChildNode("author");
    Author author = authorBuilder.build(authorNode);
    Node contentNode = getChildNode("content");
    BlogContent content = contentBuilder.build(contentNode);
    String title = getText("title");
    String summary = getText("summary");
    LinkUrl[] links = getLinks();
    return new BlogEntry(id, published, updated, appEdited, appControl, categories, title, summary, links
        , author, tt, content);
  }
}